package com.wtx.controller;

import com.wtx.entities.CommonResult;
import com.wtx.entities.Payment;
import com.wtx.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    //org.springframework.cloud.client.discovery.DiscoveryClient
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/payment/discovery")
    public Object discovery(){
        //注册到eureka server的服务名称
        List<String> services = discoveryClient.getServices();
        //获取微服务CLOUD-PAYMENT-SERVICE的全部实例，这里是 payment8001、payment8002
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        return this.discoveryClient;
    }

    @PostMapping("/payment")
    public CommonResult create(@RequestBody Payment payment){
        int i = paymentService.create(payment);
        log.info("***************插入结果："+i+"********************");
        if (i > 0) {
            return new CommonResult(200,"成功："+serverPort,i);
        }else {
            return new CommonResult(444,"失败",null);
        }
    }
    @GetMapping("/payment/{id}")
    public CommonResult get(@PathVariable("id") Integer id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("****************************查询结果："+payment);
        if (payment == null) {
            return new CommonResult(444,"失败",null);
        }else {
            return new CommonResult(200,"成功",payment);
        }
    }
}
